#!/bin/bash

# supercop/data-summarize
# D. J. Bernstein
# Public domain.

. ./data-vars

renice -n 10 $$

(
  cat OPERATIONS \
  | while read o macros prototypes
  do
    ls "$o" \
    | sort \
    | while read p
    do
      [ -d "$o" ] || continue
      okabi \
      | while read abi
      do
        [ -d "$o/$p" ] || continue
        find "$o/$p" -follow -name "api.h" \
        | sort \
        | while read doth
        do
	  implementationdir=`dirname $doth`

	  cfiles=`ls "$implementationdir" | grep '\.c$' || :`
	  sfiles=`ls "$implementationdir" | grep '\.[sS]$' || :`
	  ccfiles=`ls "$implementationdir" | grep '\.cc$' || :`
	  cppfiles=`ls "$implementationdir" | grep '\.cpp$' || :`
  
	  language=c
	  [ "x$cppfiles" = x ] || language=cpp
	  [ "x$ccfiles" = x ] || language=cpp

	  ok${language}-$abi \
	  | while read compiler
	  do
	    compilerword=`echo "$compiler" | tr ' ' '_'`
            if [ -f "$top/$abi/try/$language/$compilerword/$implementationdir/data" ]
	    then
              grep . < "$top/$abi/try/$language/$compilerword/$implementationdir/data"
	    fi
	  done
	done

        for m in 1 2 3
        do
	  if [ -f "$top/$abi/measure$m/$o/$p/data" ]
	  then
	    grep . < "$top/$abi/measure$m/$o/$p/data"
	  fi
        done
      done
    done
  done 
  sed 's/^/+ /' < "$top/log"
) | pigz -9 --rsyncable > "$top/data.gz.tmp"

mv "$top/data.gz.tmp" "$top/data.gz"
touch "$top/done-$version"
